package jp.seraph.tool.motion.runner;

import java.io.File;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;

import jp.seraph.jsade.core.AgentContext;
import jp.seraph.jsade.core.DefaultAgentContext;
import jp.seraph.jsade.core.DefaultAgentRunner;
import jp.seraph.jsade.core.DefaultWorld;
import jp.seraph.jsade.core.NopSeeObjectCalculator;
import jp.seraph.jsade.model.nao.NaoJointIdentifierContainer;
import jp.seraph.jsade.model.nao.NaoModelManager;
import jp.seraph.jsade.task.TaskSelectorAgent;
import jp.seraph.jsade.util.Executor;
import jp.seraph.jsmf.codec.FileMotionManager;
import jp.seraph.jsmf.codec.OldMotionCodec;
import jp.seraph.jspf.codec.FilePoseManager;
import jp.seraph.jspf.codec.JsonPoseCodec;

public class AgentMotionRunner {
    private static String POSE_DIR = "Pose";

    private static String MOTION_DIR = "Motion";

    /**
     * 引数に与えられたモーションを順番に実行するメソッドです。
     *
     * @param args
     */
    public static void main(String[] args) {
        if (args.length == 0) {
            System.out.println("１つ以上のモーションを指定して下さい．");
            return;
        }

        String tIPAddress = "";
        int tPort = 3100;
        tIPAddress = args[0];
        tPort = Integer.parseInt(args[1]);

        // Motionを読み込んだり
        FilePoseManager tPoseManager = new FilePoseManager(new File(POSE_DIR), new JsonPoseCodec(new NaoJointIdentifierContainer()), true);
        OldMotionCodec tCodec= new OldMotionCodec(tPoseManager, new NaoJointIdentifierContainer());
        final FileMotionManager tMotionManager = new FileMotionManager(new File(MOTION_DIR),tCodec);
        List<String> tNames = new ArrayList<String>();
        for (int i = 2; i < args.length; i++) {
            String tMotionName = args[i];
            if (!tMotionManager.containsMotion(tMotionName)) {
                System.out.println("モーション : " + tMotionName + " が見つかりません");
                return;
            }
            tNames.add(tMotionName);
        }

        // Agentのインスタンスを作成
        ListMotionSelector tSelector = new ListMotionSelector(tMotionManager, tNames);
        TaskSelectorAgent tAgent = new TaskSelectorAgent(tSelector);
        final DefaultAgentRunner tRunner = createRunner(tAgent, new InetSocketAddress(tIPAddress, tPort));

        tSelector.setExecuter(new Executor(){
            public void execute(){
                tRunner.stopAgent();
            }
        });

        // 実行
        tRunner.startAgent();

        try {
            tRunner.join();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static DefaultAgentRunner createRunner(TaskSelectorAgent aAgent, InetSocketAddress aAddress) {
        AgentContext tContext = new DefaultAgentContext(aAddress, "MotionEditorAgent", 1, AgentContext.NAO_MODEL_INITIALIZER);
        DefaultAgentRunner tRunner = new DefaultAgentRunner(new DefaultWorld(new NaoModelManager(0), new NopSeeObjectCalculator()), tContext, aAgent);

        return tRunner;
    }
}
